package com.stu.jvm.utils;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public final class UnsafeUtil {

    private  static Unsafe unsafe;

    static {
        Field[] declaredFields = Unsafe.class.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            if (declaredFields[i].getName().equals("theUnsafe")){
                declaredFields[i].setAccessible(true);
                try {
                    unsafe = (Unsafe)declaredFields[i].get(null);
                    break;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static final Unsafe  getUnsafe(){
        return unsafe;
    }



}
